#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QDebug"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow),
    buttonbeepState(false),  // 初始化为关闭状态
    buttonledState(false),
    buttonfanState(false),
    buttonmusicState(false),
    buttonbreathState(false)
{
    ui->setupUi(this);
    clientSocket = new QTcpSocket(this);
    clientSocket->connectToHost("192.168.4.1", 8888);
    if (!clientSocket->waitForConnected(5000)) {
        qDebug() << "连接失败:" << clientSocket->errorString();
    } else {
        qDebug() << "连接服务器成功";
    }

    setButtonIcon(ui->pushButton_led, ":/img/关灯.png");
    setButtonIcon(ui->pushButton_beep,":/img/蜂鸣器关.png");
    setButtonIcon(ui->pushButton_fan,":/img/风扇-关.png");
    setButtonIcon(ui->pushButton_breath,":/img/呼吸灯关 (2).png");
    setButtonIcon(ui->pushButton_music,":/img/音乐关.png");


    // 连接按钮点击信号到各自的槽函数
    connect(ui->pushButton_led, &QPushButton::clicked, this, &MainWindow::slotToggleButton_Led);
    connect(ui->pushButton_beep, &QPushButton::clicked, this, &MainWindow::slotToggleButton_Beep);
    connect(ui->pushButton_fan, &QPushButton::clicked, this, &MainWindow::slotToggleButton_Fan);
    connect(ui->pushButton_breath, &QPushButton::clicked, this, &MainWindow::slotToggleButton_breath);
    connect(ui->pushButton_music, &QPushButton::clicked, this, &MainWindow::slotToggleButton_music);

    //网络连接槽函数
  //  connect(clientSocket, &QTcpSocket::readyRead, this, &MainWindow::Readyread);
    connect(clientSocket,SIGNAL(readyRead()),this,SLOT(Readyread()));
    connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
            this, SLOT(handleSocketError(QAbstractSocket::SocketError)));

}


void MainWindow::slotToggleButton_Led()
{
    if (buttonledState) {
        QString dataToSend = "d\r\n";
        clientSocket->write(dataToSend.toLocal8Bit()); //关
        setButtonIcon(ui->pushButton_led, ":/img/关灯.png");
    } else {
        QString dataToSend = "c\r\n";
        clientSocket->write(dataToSend.toLocal8Bit()); //开
        setButtonIcon(ui->pushButton_led, ":/img/开灯.png");
    }
    buttonledState = !buttonledState;  // 切换状态
}

void MainWindow::slotToggleButton_Beep()
{
    if (buttonbeepState) {
        QString dataToSend = "b\r\n";
        clientSocket->write(dataToSend.toLocal8Bit()); //关
        setButtonIcon(ui->pushButton_beep, ":/img/蜂鸣器关.png");
    } else {
        QString dataToSend = "a\r\n";
        clientSocket->write(dataToSend.toLocal8Bit()); //开
        setButtonIcon(ui->pushButton_beep, ":/img/蜂鸣器开.png");
    }
    buttonbeepState = !buttonbeepState;  // 切换状态
}

void MainWindow::slotToggleButton_Fan()
{
    if (buttonfanState) {
        QString dataToSend = "f\r\n";
        clientSocket->write(dataToSend.toLocal8Bit()); //关
        setButtonIcon(ui->pushButton_fan, ":/img/风扇-关.png");
    } else {
        QString dataToSend = "e\r\n";
        clientSocket->write(dataToSend.toLocal8Bit()); //开
        setButtonIcon(ui->pushButton_fan, ":/img/风扇-开.png");
    }
    buttonfanState = !buttonfanState;  // 切换状态
}

void MainWindow::slotToggleButton_breath()
{
    if (buttonbreathState) {
        QString dataToSend = "h\r\n";
        clientSocket->write(dataToSend.toLocal8Bit()); //关
        setButtonIcon(ui->pushButton_breath, ":/img/呼吸灯关 (2).png");
    } else {
        QString dataToSend = "g\r\n";
        clientSocket->write(dataToSend.toLocal8Bit()); //开
        setButtonIcon(ui->pushButton_breath, ":/img/呼吸灯开.png");
    }
    buttonbreathState = !buttonbreathState;  // 切换状态
}

void MainWindow::slotToggleButton_music()
{
    if (buttonmusicState) {
        QString dataToSend = "j\r\n";
        clientSocket->write(dataToSend.toLocal8Bit()); //关
        setButtonIcon(ui->pushButton_music, ":/img/音乐关.png");
    } else {
        QString dataToSend = "i\r\n";
        clientSocket->write(dataToSend.toLocal8Bit()); //开
        setButtonIcon(ui->pushButton_music, ":/img/音乐开.png");
    }
    buttonmusicState = !buttonmusicState;  // 切换状态
}

void MainWindow::setButtonIcon(QPushButton *button, const QString &imagePath) {
    QPixmap pixmap(imagePath);
    if (pixmap.isNull()) {
        qDebug() << "图片加载失败:" << imagePath;
        return;
    }
    // 获取按钮的大小
    QSize buttonSize = button->size();
    // 根据按钮的大小调整图标的大小
    button->setIcon(QIcon(pixmap.scaled(buttonSize, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    button->setIconSize(buttonSize); // 设置图标大小为按钮大小
}

void MainWindow::handleSocketError(QAbstractSocket::SocketError socketError) {
    qDebug() << "连接错误:" << socketError << clientSocket->errorString();
}
void MainWindow::clientConnect()
{
    qDebug()<<"连接服务器成功";
}

void MainWindow::Readyread()
{
    while (clientSocket->bytesAvailable() > 0) {
        QByteArray ret = clientSocket->readAll(); // 读取所有可用字节
        qDebug() << "收到信息" << ret;

        QByteArray retCStr = ret.trimmed(); // 去除多余空格
        char *str = retCStr.data(); // 获取指向字符数组的指针

        char humi[10];
        char temp[10];
        char val[10];

        char *token = strtok(str, ":"); // 分割字符串
        if (token != nullptr) {
            strncpy(humi, token, sizeof(humi));
            humi[sizeof(humi) - 1] = '\0';
        }

        token = strtok(nullptr, ":");
        if (token != nullptr) {
            strncpy(temp, token, sizeof(temp));
            temp[sizeof(temp) - 1] = '\0';
        }

        token = strtok(nullptr, ":");
        if (token != nullptr) {
            strncpy(val, token, sizeof(val));
            val[sizeof(val) - 1] = '\0';
        }
        updateDate(humi,temp,val);
        //Show_Text(humi,temp,val);
        qDebug() << "湿度:" << humi << "温度:" << temp << "光照强度:" << val;
        Show_Text(humi,temp,val);
    }

}



void MainWindow::Show_Text(char *humi, char *temp, char *val)
{
    QString styleSheet = "QLabel {"
                         "font-size: 20px;"           // 字体大小
                         "font-weight: bold;"         // 字体加粗
                         "color: #FF0000;"            // 字体颜色，红色
                         "}";

}

void MainWindow::updateDate(char *humi, char *temp, char *val)
{
    ui->labelwendu->setText(temp);
    ui->labelshidu->setText(humi);
    ui->label_light->setText(val);
    // 设置样式
    QString styleSheet = "QLabel {"
                         "font-size: 20px;"           // 字体大小
                         "font-weight: bold;"         // 字体加粗
                         "color: #2E8BC0;"            // 字体颜色，使用16进制颜色值
                         "background-color: #F0F8FF;" // 背景颜色
                         "border: 1px solid #2E8BC0;" // 边框
                         "padding: 5px;"              // 内边距
                         "border-radius: 5px;"        // 圆角边框
                         "}";

   // 应用样式
   ui->labelwendu->setStyleSheet(styleSheet);
   ui->labelshidu->setStyleSheet(styleSheet);
   ui->label_light->setStyleSheet(styleSheet);

}

MainWindow::~MainWindow()
{
    delete ui;
}


